<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<title>src/vfs/extfs/helpers/mailfs.in</title>
<meta name='robots' content='noindex,nofollow' />
<meta name='generator' content='GLOBAL-6.5.7' />
<meta http-equiv='Content-Style-Type' content='text/css' />
<link rel='stylesheet' type='text/css' href='../style.css' />
</head>
<body>
<!-- beginning of fixed guide -->
<div id='guide'><ul>
<li><a href='#TOP'><img class='icon' src='../icons/first.png' alt='[^]' /></a></li>
<li><a href='#BOTTOM'><img class='icon' src='../icons/last.png' alt='[v]' /></a></li>
<li><a href='#TOP'><img class='icon' src='../icons/top.png' alt='[top]' /></a></li>
<li><a href='#BOTTOM'><img class='icon' src='../icons/bottom.png' alt='[bottom]' /></a></li>
<li><a href='../mains.html'><img class='icon' src='../icons/index.png' alt='[index]' /></a></li>
<li><a href='../help.html'><img class='icon' src='../icons/help.png' alt='[help]' /></a></li>
<li class='standout'><span><a href='../files/src.html'>src</a>/<a href='../files/src--vfs.html'>vfs</a>/<a href='../files/src--vfs--extfs.html'>extfs</a>/<a href='../files/src--vfs--extfs--helpers.html'>helpers</a>/mailfs.in</span></li>
</ul></div>
<!-- end of fixed guide -->
<a id='TOP' name='TOP'></a><h2 class='header'><a href='../mains.html'>root</a>/<a href='../files/src.html'>src</a>/<a href='../files/src--vfs.html'>vfs</a>/<a href='../files/src--vfs--extfs.html'>extfs</a>/<a href='../files/src--vfs--extfs--helpers.html'>helpers</a>/mailfs.in</h2>
<em class='comment'>/* <img class='icon' src='../icons/n_left.png' alt='[previous]' /><img class='icon' src='../icons/n_right.png' alt='[next]' /><img class='icon' src='../icons/n_first.png' alt='[first]' /><img class='icon' src='../icons/n_last.png' alt='[last]' /><img class='icon' src='../icons/n_top.png' alt='[top]' /><a href='#BOTTOM'><img class='icon' src='../icons/bottom.png' alt='[bottom]' /></a><a href='../mains.html'><img class='icon' src='../icons/index.png' alt='[index]' /></a><a href='../help.html'><img class='icon' src='../icons/help.png' alt='[help]' /></a>&nbsp;<input type='text' readonly onfocus='this.select();' value='+1 src/vfs/extfs/helpers/mailfs.in'  /> */</em>
<hr />
<pre>
<a id='L1' name='L1'></a>#! @PERL@ -w
<a id='L2' name='L2'></a> 
<a id='L3' name='L3'></a>use bytes;
<a id='L4' name='L4'></a> 
<a id='L5' name='L5'></a># MC extfs for (possibly compressed) Berkeley style mailbox files
<a id='L6' name='L6'></a># Peter Daum &lt;gator@cs.tu-berlin.de&gt; (Jan 1998, mc-4.1.24)
<a id='L7' name='L7'></a> 
<a id='L8' name='L8'></a>$zcat="zcat";                 # gunzip to stdout
<a id='L9' name='L9'></a>$bzcat="bzip2 -dc";           # bunzip2 to stdout
<a id='L10' name='L10'></a>$lzipcat="lzip -dc";          # unlzip to stdout
<a id='L11' name='L11'></a>$lz4cat="lz4 -dc";            # unlz4 to stdout
<a id='L12' name='L12'></a>$lzcat="lzma -dc";            # unlzma to stdout
<a id='L13' name='L13'></a>$xzcat="xz -dc";              # unxz to stdout
<a id='L14' name='L14'></a>$zstdcat="zstd -dc";          # unzstd to stdout
<a id='L15' name='L15'></a>$file="file";                 # "file" command
<a id='L16' name='L16'></a>$TZ='GMT';                    # default timezone (for Date module)
<a id='L17' name='L17'></a> 
<a id='L18' name='L18'></a>if (eval "require Date::Parse") {
<a id='L19' name='L19'></a>    import Date::Parse;
<a id='L20' name='L20'></a>    $parse_date=
<a id='L21' name='L21'></a>        sub {
<a id='L22' name='L22'></a>            local $ftime = str2time($_[0],$TZ);
<a id='L23' name='L23'></a>            $_ = localtime($ftime);
<a id='L24' name='L24'></a>            /^(...) (...) ([ \d]\d) (\d\d:\d\d):\d\d (\d\d\d\d)$/;
<a id='L25' name='L25'></a>            if ($ftime + 6 * 30 * 24 * 60 * 60 &lt; $now ||
<a id='L26' name='L26'></a>                $ftime + 60 * 60 &gt; $now) {
<a id='L27' name='L27'></a>                return "$2 $3 $5";
<a id='L28' name='L28'></a>            } else {
<a id='L29' name='L29'></a>                return "$2 $3 $4";
<a id='L30' name='L30'></a>            }
<a id='L31' name='L31'></a>        }
<a id='L32' name='L32'></a>} elsif (eval "require Date::Manip") {
<a id='L33' name='L33'></a>    import Date::Manip;
<a id='L34' name='L34'></a>    $parse_date=
<a id='L35' name='L35'></a>        sub {
<a id='L36' name='L36'></a>            return UnixDate($_[0], "%l"); # "ls -l" format
<a id='L37' name='L37'></a>        }
<a id='L38' name='L38'></a>} else {                        # use "light" version
<a id='L39' name='L39'></a>    $parse_date= sub {
<a id='L40' name='L40'></a>        local $mstring='GeeJanFebMarAprMayJunJulAugSepOctNovDec';
<a id='L41' name='L41'></a>        # assumes something like: Mon, 5 Jan 1998 16:08:19 +0200 (GMT+0200)
<a id='L42' name='L42'></a>        # if you have mails with another date format, add it here
<a id='L43' name='L43'></a>        if (/(\d\d?) ([A-Z][a-z][a-z]) (\d\d\d\d) (\d\d?):(\d\d)/) {
<a id='L44' name='L44'></a>            $day = $1;
<a id='L45' name='L45'></a>            $month = $2;
<a id='L46' name='L46'></a>            $mon = index($mstring,$month) / 3;
<a id='L47' name='L47'></a>            $year = $3;
<a id='L48' name='L48'></a>            $hour = $4;
<a id='L49' name='L49'></a>            $min = $5;
<a id='L50' name='L50'></a>            # pass time not year for files younger than roughly 6 months
<a id='L51' name='L51'></a>            # but not for files with dates more than 1-2 hours in the future
<a id='L52' name='L52'></a>            if ($year * 12 + $mon &gt; $thisyear * 12 + $thismon - 7 &amp;&amp;
<a id='L53' name='L53'></a>                $year * 12 + $mon &lt;= $thisyear * 12 + $thismon &amp;&amp;
<a id='L54' name='L54'></a>                ! (($year * 12 + $mon) * 31 + $day ==
<a id='L55' name='L55'></a>                ($thisyear * 12 + $thismon) * 31 + $thisday &amp;&amp;
<a id='L56' name='L56'></a>                $hour &gt; $thishour + 2)) {
<a id='L57' name='L57'></a>                return "$month $day $hour:$min";
<a id='L58' name='L58'></a>            } else {
<a id='L59' name='L59'></a>                return "$month $day $year";
<a id='L60' name='L60'></a>            }
<a id='L61' name='L61'></a>        }
<a id='L62' name='L62'></a>        # Y2K bug.
<a id='L63' name='L63'></a>        # Date: Mon, 27 Mar 100 16:30:47 +0000 (GMT)
<a id='L64' name='L64'></a>        if (/(\d\d?) ([A-Z][a-z][a-z]) (1?\d\d) (\d\d?):(\d\d)/) {
<a id='L65' name='L65'></a>            $day = $1;
<a id='L66' name='L66'></a>            $month = $2;
<a id='L67' name='L67'></a>            $mon = index($mstring,$month) / 3;
<a id='L68' name='L68'></a>            $year = 1900 + $3;
<a id='L69' name='L69'></a>            $hour = $4;
<a id='L70' name='L70'></a>            $min = $5;
<a id='L71' name='L71'></a>            if ($year &lt; 1970) {
<a id='L72' name='L72'></a>                $year += 100;
<a id='L73' name='L73'></a>            }
<a id='L74' name='L74'></a>            if ($year * 12 + $mon &gt; $thisyear * 12 + $thismon - 7 &amp;&amp;
<a id='L75' name='L75'></a>                $year * 12 + $mon &lt;= $thisyear * 12 + $thismon &amp;&amp;
<a id='L76' name='L76'></a>                ! (($year * 12 + $mon) * 31 + $day ==
<a id='L77' name='L77'></a>                ($thisyear * 12 + $thismon) * 31 + $thisday &amp;&amp;
<a id='L78' name='L78'></a>                $hour &gt; $thishour + 2)) {
<a id='L79' name='L79'></a>                return "$month $day $hour:$min";
<a id='L80' name='L80'></a>            } else {
<a id='L81' name='L81'></a>                return "$month $day $year";
<a id='L82' name='L82'></a>            }
<a id='L83' name='L83'></a>        }
<a id='L84' name='L84'></a>        # AOLMail(SM).
<a id='L85' name='L85'></a>        # Date: Sat Jul 01 10:06:06 2000
<a id='L86' name='L86'></a>        if (/([A-Z][a-z][a-z]) (\d\d?) (\d\d?):(\d\d)(:\d\d)? (\d\d\d\d)/) {
<a id='L87' name='L87'></a>            $month = $1;
<a id='L88' name='L88'></a>            $mon = index($mstring,$month) / 3;
<a id='L89' name='L89'></a>            $day = $2;
<a id='L90' name='L90'></a>            $hour = $3;
<a id='L91' name='L91'></a>            $min = $4;
<a id='L92' name='L92'></a>            $year = $6;
<a id='L93' name='L93'></a>            if ($year * 12 + $mon &gt; $thisyear * 12 + $thismon - 7 &amp;&amp;
<a id='L94' name='L94'></a>                $year * 12 + $mon &lt;= $thisyear * 12 + $thismon &amp;&amp;
<a id='L95' name='L95'></a>                ! (($year * 12 + $mon) * 31 + $day ==
<a id='L96' name='L96'></a>                ($thisyear * 12 + $thismon) * 31 + $thisday &amp;&amp;
<a id='L97' name='L97'></a>                $hour &gt; $thishour + 2)) {
<a id='L98' name='L98'></a>                return "$month $day $hour:$min";
<a id='L99' name='L99'></a>            } else {
<a id='L100' name='L100'></a>                return "$month $day $year";
<a id='L101' name='L101'></a>            }
<a id='L102' name='L102'></a>        }
<a id='L103' name='L103'></a>        # Fallback
<a id='L104' name='L104'></a>        return $fallback;
<a id='L105' name='L105'></a>    }
<a id='L106' name='L106'></a>}
<a id='L107' name='L107'></a> 
<a id='L108' name='L108'></a>sub process_header {
<a id='L109' name='L109'></a>    while (&lt;IN&gt;) {
<a id='L110' name='L110'></a>        $size+=length;
<a id='L111' name='L111'></a>        s/\r$//;
<a id='L112' name='L112'></a>        last if /^$/;
<a id='L113' name='L113'></a>        die "unexpected EOF\n" if eof;
<a id='L114' name='L114'></a>        if (/^date:\s(.*)$/i) {
<a id='L115' name='L115'></a>            $date=&amp;$parse_date($1);
<a id='L116' name='L116'></a>        } elsif (/^subject:\s(.*)$/i) {
<a id='L117' name='L117'></a>            $subj=lc($1);
<a id='L118' name='L118'></a>            $subj=~ s/^(re:\s?)+//gi;  # no leading Re:
<a id='L119' name='L119'></a>            $subj=~ tr/a-zA-Z0-9//cd; # strip all "special" characters
<a id='L120' name='L120'></a>        } elsif (/^from:\s.*?(\w+)\@/i) {
<a id='L121' name='L121'></a>            $from=$1;
<a id='L122' name='L122'></a>        } elsif (/^to:\s.*?(\w+)\@/i) {
<a id='L123' name='L123'></a>            $to=lc($1);
<a id='L124' name='L124'></a>        }
<a id='L125' name='L125'></a>    }
<a id='L126' name='L126'></a>}
<a id='L127' name='L127'></a> 
<a id='L128' name='L128'></a>sub print_dir_line {
<a id='L129' name='L129'></a>    $from=$to if ($from eq $user); # otherwise, it would look pretty boring
<a id='L130' name='L130'></a>    $date=localtime(time) if (!defined $date);
<a id='L131' name='L131'></a>    printf "-r-------- 1 $&lt; $&lt; %d %s %3.3d_%.25s\n",
<a id='L132' name='L132'></a>    $size, $date, $msg_nr, "${from}_${subj}";
<a id='L133' name='L133'></a> 
<a id='L134' name='L134'></a>}
<a id='L135' name='L135'></a> 
<a id='L136' name='L136'></a>sub mailfs_list {
<a id='L137' name='L137'></a>    my $blank = 1;
<a id='L138' name='L138'></a>    $user=$ENV{USER}||getlogin||getpwuid($&lt;) || "nobody";
<a id='L139' name='L139'></a> 
<a id='L140' name='L140'></a>    while(&lt;IN&gt;) {
<a id='L141' name='L141'></a>        s/\r$//;
<a id='L142' name='L142'></a>        if($blank &amp;&amp; /^from\s+\w+(\.\w+)*@/i) { # Start of header
<a id='L143' name='L143'></a>            print_dir_line unless (!$msg_nr);
<a id='L144' name='L144'></a>            $size=length;
<a id='L145' name='L145'></a>            $msg_nr++;
<a id='L146' name='L146'></a>            ($from,$to,$subj,$date)=("none","none","none", "01-01-80");
<a id='L147' name='L147'></a>            process_header;
<a id='L148' name='L148'></a>            $line=$blank=0;
<a id='L149' name='L149'></a>        } else {
<a id='L150' name='L150'></a>            $size+=length;
<a id='L151' name='L151'></a>            $line++;
<a id='L152' name='L152'></a>            $blank= /^$/;
<a id='L153' name='L153'></a>        }
<a id='L154' name='L154'></a>    }
<a id='L155' name='L155'></a>    print_dir_line unless (!$msg_nr);
<a id='L156' name='L156'></a>    exit 0;
<a id='L157' name='L157'></a>}
<a id='L158' name='L158'></a> 
<a id='L159' name='L159'></a>sub mailfs_copyout {
<a id='L160' name='L160'></a>    my($source,$dest)=@_;
<a id='L161' name='L161'></a>    exit 1 unless (open STDOUT, "&gt;$dest");
<a id='L162' name='L162'></a>    ($nr)= ($source =~ /^(\d+)/); # extract message number from "filename"
<a id='L163' name='L163'></a> 
<a id='L164' name='L164'></a>    my $blank = 1;
<a id='L165' name='L165'></a>    while(&lt;IN&gt;) {
<a id='L166' name='L166'></a>        s/\r$//;
<a id='L167' name='L167'></a>        if($blank &amp;&amp; /^from\s+\w+(\.\w+)*@/i) {
<a id='L168' name='L168'></a>            $msg_nr++;
<a id='L169' name='L169'></a>            exit(0) if ($msg_nr &gt; $nr);
<a id='L170' name='L170'></a>            $blank= 0;
<a id='L171' name='L171'></a>        } else {
<a id='L172' name='L172'></a>            $blank= /^$/;
<a id='L173' name='L173'></a>        }
<a id='L174' name='L174'></a>        print if ($msg_nr == $nr);
<a id='L175' name='L175'></a>    }
<a id='L176' name='L176'></a>}
<a id='L177' name='L177'></a> 
<a id='L178' name='L178'></a># main {
<a id='L179' name='L179'></a>exit 1 unless ($#ARGV &gt;= 1);
<a id='L180' name='L180'></a>$msg_nr=0;
<a id='L181' name='L181'></a>$cmd=shift;
<a id='L182' name='L182'></a>$mbox_name=shift;
<a id='L183' name='L183'></a>my $mbox_qname = quotemeta ($mbox_name);
<a id='L184' name='L184'></a>$_=`$file $mbox_qname`;
<a id='L185' name='L185'></a> 
<a id='L186' name='L186'></a>if (/gzip/) {
<a id='L187' name='L187'></a>    exit 1 unless (open IN, "$zcat $mbox_qname|");
<a id='L188' name='L188'></a>} elsif (/bzip/) {
<a id='L189' name='L189'></a>    exit 1 unless (open IN, "$bzcat $mbox_qname|");
<a id='L190' name='L190'></a>} elsif (/lzip/) {
<a id='L191' name='L191'></a>    exit 1 unless (open IN, "$lzipcat $mbox_qname|");
<a id='L192' name='L192'></a>} elsif (/lz4/) {
<a id='L193' name='L193'></a>    exit 1 unless (open IN, "$lz4cat $mbox_qname|");
<a id='L194' name='L194'></a>} elsif (/lzma/) {
<a id='L195' name='L195'></a>    exit 1 unless (open IN, "$lzcat $mbox_qname|");
<a id='L196' name='L196'></a>} elsif (/xz/) {
<a id='L197' name='L197'></a>    exit 1 unless (open IN, "$xzcat $mbox_qname|");
<a id='L198' name='L198'></a>} elsif (/zst/) {
<a id='L199' name='L199'></a>    exit 1 unless (open IN, "$zstdcat $mbox_qname|");
<a id='L200' name='L200'></a>} else {
<a id='L201' name='L201'></a>    exit 1 unless (open IN, "&lt;$mbox_name");
<a id='L202' name='L202'></a>}
<a id='L203' name='L203'></a> 
<a id='L204' name='L204'></a>umask 077;
<a id='L205' name='L205'></a> 
<a id='L206' name='L206'></a>if($cmd eq "list") {
<a id='L207' name='L207'></a>    $now = time;
<a id='L208' name='L208'></a>    $_ = localtime($now);
<a id='L209' name='L209'></a>    /^... (... [ \d]\d \d\d:\d\d):\d\d \d\d\d\d$/;
<a id='L210' name='L210'></a>    $fallback = $1;
<a id='L211' name='L211'></a>    $nowstring=`date "+%Y %m %d %H"`;
<a id='L212' name='L212'></a>    ($thisyear, $thismon, $thisday, $thishour) = split(/ /, $nowstring);
<a id='L213' name='L213'></a>    &amp;mailfs_list;
<a id='L214' name='L214'></a>    exit 0;
<a id='L215' name='L215'></a>}
<a id='L216' name='L216'></a>elsif($cmd eq "copyout") { &amp;mailfs_copyout(@ARGV); exit 0; }
<a id='L217' name='L217'></a> 
<a id='L218' name='L218'></a>exit 1;
</pre>
<hr />
<a id='BOTTOM' name='BOTTOM'></a>
<em class='comment'>/* <img class='icon' src='../icons/n_left.png' alt='[previous]' /><img class='icon' src='../icons/n_right.png' alt='[next]' /><img class='icon' src='../icons/n_first.png' alt='[first]' /><img class='icon' src='../icons/n_last.png' alt='[last]' /><a href='#TOP'><img class='icon' src='../icons/top.png' alt='[top]' /></a><img class='icon' src='../icons/n_bottom.png' alt='[bottom]' /><a href='../mains.html'><img class='icon' src='../icons/index.png' alt='[index]' /></a><a href='../help.html'><img class='icon' src='../icons/help.png' alt='[help]' /></a>&nbsp;<input type='text' readonly onfocus='this.select();' value='+218 src/vfs/extfs/helpers/mailfs.in'  /> */</em>
</body>
</html>
